<?php

namespace app\admin\controller;

use app\_interface\ResourceInterface;
use app\common\entity\ResponseData;
use Sffi\ElementPlus\ElForm;
use Sffi\ElementPlus\ElList;
use Sffi\ElementPlus\ElRule\Law;
use Sffi\ElementPlus\ElRule\Rule;
use Sffi\ElementPlus\ElSearch\ElInput;
use think\facade\Request;

class RoutesApi extends \app\admin\controller\AdminBase  implements ResourceInterface
{

    public function list()
    {
        $model = \app\model\RoutesApi::when($routesId = input('routes_id'), fn($query) => $query->where('routes_id', '=', $routesId));
        $count = $model->count();
        return ResponseData::success($model->select(),['sum'=>$count, 'page'=>input('page', 1),'size'=>input('size', 10)]);
    }

    public function detail()
    {
        $id = input('id', 0);
        return ResponseData::success(\app\model\RoutesApi::find($id));
    }

    public function add()
    {
        $data =  Request::only(['name','module','controller','action','routes_id','status']);
        $api = \app\model\RoutesApi::where(['module'=>$data['module'], 'controller'=>$data['controller'],'action'=>$data['action']])->find();
        if (!$api) {
            $api = \app\model\RoutesApi::create($data);
            return ResponseData::success($api);
        }
        return ResponseData::error('该接口已存在');
    }

    public function update()
    {
        $id = input('id');
        $data =  Request::only(['name','module','controller','action','routes_id','status']);
        $api = \app\model\RoutesApi::find($id);
        if (!$api) {
            return ResponseData::error('该接口不存在');
        }
        return ResponseData::success($api->save($data));
    }

    public function delete($id)
    {
        $id = input('id');
        \app\model\RoutesApi::where('id', '=', $id)->delete();
        return ResponseData::success(1);
    }

    public function getListColumns()
    {
        $list = new ElList();
        $list->addColumn(new ElList\Column('名称', 'name','name', 150))
            ->addColumn(new ElList\Column('模块', 'module','module'))
            ->addColumn(new ElList\Column('控制器', 'controller', 'controller'))
            ->addColumn(new ElList\Column('action','action','action'))
            ->addSearch(new ElInput('名称','name','请输入接口名称'));
        return ResponseData::success($list->__toArray());
    }

    public function getFormColumns()
    {
        $form = new ElForm();
        $select = new ElForm\ElSelect('所属菜单','routes_id');
        $routes = \app\model\Routes::where('pid', '<>', 0)->where('status', '=', 1)->select();
        foreach ($routes as $route) {
            $select->addOptionByArgs($route->name, $route->id);
        }
        $rule = new Rule('name');
        $law = new Law('请填写菜单名称');
        $law->setRequired(true);
        $form->addColumn(new ElForm\ElInput('接口名', 'name', '请输入接口名称'))
            ->addColumn(new ElForm\ElInput('模块名', 'module', '请输入模块名'))
            ->addColumn(new ElForm\ElInput('模块名', 'controller', '请输入控制器名'))
            ->addColumn(new ElForm\ElInput('模块名', 'action', '请输入接口action'))
            ->addColumn($select)
            ->addColumn(new ElForm\ElSwitch('状态', 'status',1, 0, '有效','无效'))
            ->addRule($rule->addLaw($law));
        return ResponseData::success($form->__toArray());
    }
}